﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI;
using KPIS.GERP.Models.SYS;

namespace KPIS.GERP.HtmlHelpers
{
    public static class SiteNavigateHelper
    {
        public static IHtmlString SiteNavigate(this HtmlHelper helper, List<MenuItem> menuItems)
        {
            var writer = new HtmlTextWriter(new StringWriter());
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
            var defaultUrl = urlHelper.Action("Index", "Home"); 
            var currentUrl = MapTodefaultRoute(helper, HttpContext.Current.Request.RawUrl);
            
            // Render Top Ul
            writer.WriteLine();
            writer.AddAttribute("class", "breadcrumb");
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            writer.WriteLine();

            // Render item Main
            if (IsThisMainPage(helper)) writer.AddAttribute("class", "active");
            writer.RenderBeginTag(HtmlTextWriterTag.Li);

            writer.AddAttribute("class", "icon-home");
            writer.RenderBeginTag(HtmlTextWriterTag.I);
            writer.RenderEndTag();

            writer.AddAttribute("href", defaultUrl);
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("หน้าหลัก");
            writer.RenderEndTag();
            
            writer.AddAttribute("class", "divider");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.AddAttribute("class", "icon-angle-right");
            writer.RenderBeginTag(HtmlTextWriterTag.I);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag(); // Li - #Render item Main
            writer.WriteLine();

            var aaa = menuItems;
            var menuItemSelected = menuItems.FirstOrDefault(o => MapTodefaultRoute(helper, o.Url).ToLower() == currentUrl.ToLower());
            
            if(menuItemSelected != null)
            {
                //MenuItem headerOfMenuItemSelected = aaa.FirstOrDefault(m => m.SubMenu.Any(o => o.MenuItemId == menuItemSelected.MenuItemId));

                MenuItem headerOfMenuItemSelected = aaa.Where(m => m.SubMenu != null).ToList().Where(m => m.SubMenu.Any(o => o.MenuItemId == menuItemSelected.MenuItemId)).FirstOrDefault();   //FirstOrDefault(m => m.SubMenu.Any(o => o.MenuItemId == menuItemSelected.MenuItemId));

                if(menuItemSelected.MenuLevel == MenuLevel.Sub && headerOfMenuItemSelected != null)
                {
                    writer.AddAttribute("class", "active");
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);

                    writer.AddAttribute("href", headerOfMenuItemSelected.Url);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.Write(headerOfMenuItemSelected.Name);
                    writer.RenderEndTag();
                        
                    writer.AddAttribute("class", "divider");
                    writer.RenderBeginTag(HtmlTextWriterTag.Span);
                    writer.AddAttribute("class", "icon-angle-right");
                    writer.RenderBeginTag(HtmlTextWriterTag.I);
                    writer.RenderEndTag();
                    writer.RenderEndTag();

                    writer.RenderEndTag();
                    writer.WriteLine();
                }

                writer.AddAttribute("class", "active");
                writer.RenderBeginTag(HtmlTextWriterTag.Li);
                writer.Write(helper.ViewBag.Title ?? string.Empty);
                writer.RenderEndTag();
            }

            writer.RenderEndTag();

            return helper.Raw(writer.InnerWriter.ToString());
        }

        public static bool IsThisMainPage(this HtmlHelper helper)
        {
            var currentUrl = MapTodefaultRoute(helper, HttpContext.Current.Request.RawUrl);

            var defaultRoute = MenuHelper.GetDefaultRouteObject(helper);
            var defaultRouteUrl = "/" + defaultRoute["controller"]  + "/" + defaultRoute["action"] ;

            return currentUrl.ToLower().StartsWith(defaultRouteUrl.ToLower());
        }

        public static string MapTodefaultRoute(HtmlHelper helper, string url)
        {
            var currentUrl = url;
            var currentUrlSubStr = currentUrl.Split('/');
            var controllerName = currentUrlSubStr.ElementAtOrDefault(1);
            var actionName = currentUrlSubStr.ElementAtOrDefault(2);


            var defaultRoute = MenuHelper.GetDefaultRouteObject(helper);
            url = "/" + (string.IsNullOrEmpty(controllerName) ? defaultRoute["controller"] : controllerName) +
                "/" + (string.IsNullOrEmpty(actionName) ? defaultRoute["action"] : actionName);

            return url;
        }
    }
}